﻿package com.teaphy.coroutinecrawler.channel

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val channel = produceNumbers()

    launch {
        while (true) {
            println("接收到的数据为：${channel.receive()}")
        }
    }

    println("Done")
}

fun CoroutineScope.produceNumbers() = produce<Int> {
    var x = 1 // 从 1 开始
    while (true) {
        send(x++) // 产生下一个数字
        delay(1000) // 等待 1 秒
    }
}